package com.itqf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itqf.bean.DtsAdmin;
import com.itqf.bean.DtsPermission;
import com.itqf.bean.DtsRole;
import com.itqf.dto.DtsPermissionDto;
import com.itqf.dto.DtsSearchDto;
import com.itqf.mapper.DtsPermissionMapper;
import com.itqf.mapper.DtsRoleMapper;
import com.itqf.service.DtsRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * projectName: shop_manage
 *
 * @author: MaTianWei
 * time: 2021/7/13 14:26
 * description:
 */
@Service
public class DtsRoleServiceImpl implements DtsRoleService {
    @Autowired
    private DtsRoleMapper dtsRoleMapper;
    @Autowired
    private DtsPermissionMapper dtsPermissionMapper;
    public static final Logger logger = LoggerFactory.getLogger(DtsRoleServiceImpl.class);
    @Override
    public List<String> queryRoleNames(Integer[] roleIds) {
        if (roleIds == null || roleIds.length == 0) {
            logger.warn("[角色查询业务]roles为null");
            return null;
        }
        List<String>roles = dtsRoleMapper.queryRoleNameByRoleIds(roleIds);
        return roles;
    }





    @Override
    public List<Map> findRoleName() {
        List<Map> roleList = dtsRoleMapper.queryRoleName();

        logger.debug("[角色列表]业务查询数据为:{}",roleList);

        return roleList;
    }


    //角色分页查询
    @Override
    public Map findRoleByPage(DtsSearchDto roles) {
        logger.info("[角色查询]业务开始调用获取数据为:{}",roles);
        //1·分页插件启动
        PageHelper.startPage(roles.getPage(),roles.getLimit());
        //2·数据库查询
        List<DtsRole> roleList = dtsRoleMapper.queryRoleByPage(roles);
        //3·pageInfo封装
        PageInfo<DtsRole> pageInfo = new PageInfo<>(roleList);
        //4·封装返回结果数据
        Map data = new HashMap();
        data.put("total",pageInfo.getTotal());
        data.put("items",pageInfo.getList());
        logger.info("[角色查询]业务调用完毕,获取数据为:{}",data);
        return data;
    }






    //角色添加
    @Override
    public boolean checkUserName(String rolename) {
        Long count = dtsRoleMapper.queryCountByRoleName(rolename);
        logger.debug("[用户查询]username={}的账号已存在数量为:{}",rolename,count);
        return count >= 1;

    }






    //保存角色
    @Override
    public DtsRole saveRoles(DtsRole roles) {
        //1·完善admin的数据 补全没有的数据
        roles.setAddTime(new Date());
        roles.setDeleted(false);
        //2·需要修改的数据
        logger.debug("[保存用户]业务内部获取用户的密码为:{}",roles.getName());
        //3·插入数据库
        int rows = dtsRoleMapper.insertRole(roles);
        //4·返回
        return roles;
    }





    //角色更新
    @Override
    public int updateRoles(DtsRole roles) {
        roles.setUpdateTime(new Date());
        int rows = dtsRoleMapper.deleteByPrimaryKey(roles.getId());
        logger.debug("[修改角色]业务修改影响数据的形象行数rows:{}",rows);
        return rows;
    }
    @Override
    public int removeRoleById(Integer id) {
        int rows = dtsRoleMapper.deleteByPrimaryKey(id);
        return rows;
    }





    //权限管理
    @Override
    public boolean checkSuper(int roleId) {
        Long count = dtsRoleMapper.querySuper(roleId);
        logger.info("[检查超级管理员业务]调用结束,查询当前角色:{},对应管理员数据:{}",roleId,count);
        return count >= 1;
    }





    //角色权限查询
    @Override
    public Set<String> queryPerission(int roleId) {
        Set<String> permissions = dtsRoleMapper.queryPermissions(roleId);
        logger.info("[查询角色权限]查看角色id:{},对应权限为:{}",roleId,permissions);
        return permissions;
    }





    //角色权限授予
    @Override
    public void removePermission(int roleId) {
        dtsPermissionMapper.deleteByRoleId(roleId);
    }





    //角色权限插入
    @Override
    public void save(DtsPermissionDto permissionDto) {
        for(String permission : permissionDto.getPermissions()){
            DtsPermission dtsPermission = new DtsPermission();
            dtsPermission.setPermission(permission);
            dtsPermission.setRoleId(permissionDto.getRoleId());
            dtsPermission.setDeleted(false);
            dtsPermission.setAddTime(new Date());
            dtsPermissionMapper.insert(dtsPermission);
        }
    }
}
